package com.southwind.service.impl;

import com.baomidou.mybatisplus.core.assist.ISqlRunner;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.southwind.entity.TbAdmin;
import com.southwind.entity.TbAssetRole;
import com.southwind.form.RuleForm;
import com.southwind.form.SearchForm;
import com.southwind.mapper.TbAdminMapper;
import com.southwind.mapper.TbAssetRoleMapper;
import com.southwind.service.TbAssetRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.southwind.vo.PageVO;
import com.southwind.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fjh
 * @since 2024-01-08
 */
@Service
public class TbAssetRoleServiceImpl extends ServiceImpl<TbAssetRoleMapper, TbAssetRole> implements TbAssetRoleService {
    @Autowired
    private TbAssetRoleMapper  tbAssetRoleMapper;


    @Override
    public ResultVO login(RuleForm ruleForm) {
        /*1,判断用户是否存在*/
        QueryWrapper<TbAssetRole> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",ruleForm.getUsername());
        TbAssetRole tbAssetRole= this.tbAssetRoleMapper.selectOne(queryWrapper);
        ResultVO resultVO=new ResultVO();
        if (tbAssetRole==null){
            resultVO.setCode(-1);
        }else {
            /**2,判断密码是否正确*/
            if(!tbAssetRole.getPassword().equals(ruleForm.getPassword())){
                resultVO.setCode(-2);
            }else {
                resultVO.setCode(0);
                resultVO.setData(tbAssetRole);
            }
        }
        return resultVO;
    }
    @Override
    public PageVO list(Integer page, Integer size) {
        Page<TbAssetRole> dormitoryAdminPage = new Page<>(page, size);
        Page<TbAssetRole> resultPage = this.tbAssetRoleMapper.selectPage(dormitoryAdminPage, null);
        PageVO pageVO = new PageVO();
        pageVO.setTotal(resultPage.getTotal());
        pageVO.setData(resultPage.getRecords());
        return pageVO;
    }

    @Override
    public PageVO search(SearchForm searchForm) {
//        模糊查询+分页
        Page<TbAssetRole> tbAssetRolePage = new Page<>(searchForm.getPage(), searchForm.getSize());
        Page<TbAssetRole> resultPage = null;
        if(searchForm.getValue().equals("")){
            resultPage = this.tbAssetRoleMapper.selectPage(tbAssetRolePage, null);
        } else {
            QueryWrapper<TbAssetRole> queryWrapper = new QueryWrapper<>();
            queryWrapper.like(searchForm.getKey(), searchForm.getValue());
            resultPage = this.tbAssetRoleMapper.selectPage(tbAssetRolePage,queryWrapper);
        }
        PageVO pageVO = new PageVO();
        pageVO.setTotal(resultPage.getTotal());
        pageVO.setData(resultPage.getRecords());
        return pageVO;
    }
}
